package com.huawei.hwclouds.lubanops.demo.shopping.cart.biz.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huawei.hwclouds.lubanops.demo.cache.biz.service.Cache;
import com.huawei.hwclouds.lubanops.demo.common.biz.Mapper;
import com.huawei.hwclouds.lubanops.demo.common.biz.PageList;
import com.huawei.hwclouds.lubanops.demo.common.json.JsonUtil;
import com.huawei.hwclouds.lubanops.demo.order.client.OrderServiceClient;
import com.huawei.hwclouds.lubanops.demo.shopping.cart.biz.mapper.ShoppingCartsMapper;
import com.huawei.hwclouds.lubanops.demo.shopping.cart.biz.model.ShoppingCartsModel;
import com.huawei.hwclouds.lubanops.demo.shopping.cart.biz.param.ShoppingCartsParam;

/**
 * @author y00438022
 * @since 2020/8/17
 **/
@Service
public class ShoppingCartService extends ConfigBizServiceBase<ShoppingCartsModel, ShoppingCartsParam> {

    @Autowired
    private ShoppingCartsMapper mapper;

    @Autowired
    private Cache<String> cache;

    @Autowired
    private Cache<String> lettuceCache;

    @Override
    public Mapper<ShoppingCartsModel, ShoppingCartsParam> getMapper() {
        return mapper;
    }

    @Override
    public void create(ShoppingCartsModel model) {
        List<ShoppingCartsModel> models;
        lettuceCache.get(model.getUserId());
        String shopping = cache.get(model.getUserId());
        if (StringUtils.isEmpty(shopping)) {
            models = new ArrayList<>();
        } else {
            try {
                models = JsonUtil.parseList(shopping, ShoppingCartsModel.class);
            } catch (Exception exception) {
                models = new ArrayList<>();
                cache.delete(model.getUserId());
            }
        }
        models.add(model);
        cache.set(model.getUserId(), JsonUtil.toJSONString(models), 5, TimeUnit.DAYS);
        super.create(model);
    }

    @Override
    public Integer update(ShoppingCartsModel model) {
        return super.update(model);
    }

    @Override
    public PageList<ShoppingCartsModel> pageQuery(ShoppingCartsParam param) {
        return super.pageQuery(param);
    }

    @Override
    public ShoppingCartsModel retrieveById(Long id) {
        return super.retrieveById(id);
    }

    @Override
    public List<ShoppingCartsModel> findAll() {
        return super.findAll();
    }

    /**
     * 购物车结算
     *
     * @param models
     */
    public void settleAccounts(List<ShoppingCartsModel> models) {
        List<Map<String, Object>> ordersModels = new ArrayList<>();
        models.forEach(param -> {
                mapper.delete(param.getId());
                Map<String, Object> model = new HashMap<>();
                model.put("merchandiseId", param.getMerchandiseId());
                model.put("actualPayment", param.getPrice());
                model.put("num", param.getNum());
                model.put("userId", param.getUserId());
                ordersModels.add(model);
            }
        );
        ShoppingCartsParam cartParam = new ShoppingCartsParam();
        cache.delete(models.get(0).getUserId());
        cartParam.setUserId(models.get(0).getUserId());
        OrderServiceClient.addOrders(JsonUtil.toJSONString(ordersModels));
    }

    public List<ShoppingCartsModel> queryByUserId(String userId) {
        String shopping = cache.get(userId);
        lettuceCache.get(userId);
        return JsonUtil.parseList(shopping, ShoppingCartsModel.class);
    }

    public void clear() {
        mapper.clear();
    }

    public ShoppingCartsModel errorQuery(int id) {
        return mapper.errorQuery(id);
    }
}
